package com.coolgor.axnut.service.mdmis.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.coolgor.axnut.dao.mdmis.DeliverInfoDao;
import com.coolgor.axnut.dao.mdmis.DeliverInfoImportDao;
import com.coolgor.axnut.dto.mdmis.DeliverInfoExecution;
import com.coolgor.axnut.entity.mdmis.DeliverInfo;
import com.coolgor.axnut.entity.mdmis.DeliverInfoCondition;
import com.coolgor.axnut.enums.mdmis.DeliverInfoOperationStateEnum;
import com.coolgor.axnut.exceptions.mdmis.DeliverInfoOperationException;
import com.coolgor.axnut.service.mdmis.DeliverInfoService;
import com.coolgor.axnut.util.PageCalculator;

@Service
public class DeliverInfoServiceImpl implements DeliverInfoService {
	private static Logger log = LoggerFactory.getLogger(DeliverInfoServiceImpl.class);
	
	@Autowired
	private DeliverInfoDao deliverInfoDao;	
	@Autowired
	private DeliverInfoImportDao deliverInfoImportDao;


	@Override
	public DeliverInfo getDeliverInfo(Long recordID) {
		return deliverInfoDao.queryDeliverInfoByID(recordID);
	}	
	
	@Override
	public DeliverInfo getDeliverLastInfo(String insureNo) {
		return deliverInfoDao.queryDeliverLastInfo(insureNo);
	}

	@Override
	public DeliverInfoExecution getDeliverInfoList(DeliverInfo deliverInfoCondition, int pageIndex, int pageSize) {
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//依据查询条件，调用dao层返回相关的查询结果列表
		List<DeliverInfo> list = deliverInfoDao.queryDeliverInfoList(deliverInfoCondition,rowIndex,pageSize);
		//依据相同的查询条件，返回查询结果总数
		int count = deliverInfoDao.queryDeliverInfoCount(deliverInfoCondition);
		DeliverInfoExecution result = new DeliverInfoExecution();
		if (list != null) {
			result.setState(DeliverInfoOperationStateEnum.SUCCESS.getState());
			result.setStateInfo(DeliverInfoOperationStateEnum.SUCCESS.getStateInfo());
			result.setList(list);
			result.setCount(count);
		} else {
			result.setState(DeliverInfoOperationStateEnum.INNER_ERROR.getState());
		}
		return result;
	}
	
	@Override
	public DeliverInfoExecution getDeliverInfoList(DeliverInfoCondition deliverInfoCondition, int pageIndex, int pageSize) {
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//依据查询条件，调用dao层返回相关的查询结果列表
		List<DeliverInfo> list = deliverInfoDao.queryDeliverInfoListByCondition(deliverInfoCondition,rowIndex,pageSize);
		//依据相同的查询条件，返回查询结果总数
		int count = deliverInfoDao.queryDeliverInfoCountByCondition(deliverInfoCondition);
		DeliverInfoExecution result = new DeliverInfoExecution();
		if (list != null) {
			result.setState(DeliverInfoOperationStateEnum.SUCCESS.getState());
			result.setStateInfo(DeliverInfoOperationStateEnum.SUCCESS.getStateInfo());
			result.setList(list);
			result.setCount(count);
		} else {
			result.setState(DeliverInfoOperationStateEnum.INNER_ERROR.getState());
		}
		return result;
	}
	
	@Override
	public DeliverInfoExecution getDeliverInfoListFuzzy(DeliverInfo deliverInfoCondition, int pageIndex, int pageSize) {
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//依据查询条件，调用dao层返回相关的查询结果列表
		List<DeliverInfo> list = deliverInfoDao.queryDeliverInfoListFuzzy(deliverInfoCondition,rowIndex,pageSize);
		//依据相同的查询条件，返回查询结果总数
		int count = deliverInfoDao.queryDeliverInfoCountFuzzy(deliverInfoCondition);
		DeliverInfoExecution result = new DeliverInfoExecution();
		if (list != null) {
			result.setState(DeliverInfoOperationStateEnum.SUCCESS.getState());
			result.setStateInfo(DeliverInfoOperationStateEnum.SUCCESS.getStateInfo());
			result.setList(list);
			result.setCount(count);
		} else {
			result.setState(DeliverInfoOperationStateEnum.INNER_ERROR.getState());
		}
		return result;
	}

	@Override
	@Transactional
	public DeliverInfoExecution addDeliverInfo(DeliverInfo deliverInfo) {
		// 空值判断
		if (deliverInfo == null) {
			return new DeliverInfoExecution(DeliverInfoOperationStateEnum.NULL);
		}
		int effectedNum;
		try {
			// 添加
			effectedNum = deliverInfoDao.insertDeliverInfo(deliverInfo);
			if (effectedNum <= 0) {
				throw new DeliverInfoOperationException("保单配送信息创建失败");
			} 
		} catch (Exception e) {
			throw new DeliverInfoOperationException("Add deliver information error:" + e.getMessage());
		}
		return new DeliverInfoExecution(DeliverInfoOperationStateEnum.SUCCESS, deliverInfo);
	}

	@Override
	@Transactional
	public DeliverInfoExecution modifyDeliverInfo(DeliverInfo deliverInfo) {
		// 空值判断
		if (deliverInfo == null) {
			return new DeliverInfoExecution(DeliverInfoOperationStateEnum.NULL);
		}
		int effectedNum;
		// 即将更新信息
		try {
			// 更新操作
			effectedNum = deliverInfoDao.updateDeliverInfo(deliverInfo);
			if (effectedNum <= 0) {
				throw new DeliverInfoOperationException("保单配送信息更新失败");
			} 
		} catch (Exception e) {
			throw new DeliverInfoOperationException("Modify policy deliver Information error:" + e.getMessage());
		}
		return new DeliverInfoExecution(DeliverInfoOperationStateEnum.SUCCESS, deliverInfo);
	}

	@Override
	@Transactional
	public DeliverInfoExecution removeDeliverInfo(DeliverInfo deliverInfo) {
		// 空值判断
		if (deliverInfo == null) {
			return new DeliverInfoExecution(DeliverInfoOperationStateEnum.NULL);
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = deliverInfoDao.deleteDeliverInfo(deliverInfo);
			if (effectedNum <= 0) {
				throw new DeliverInfoOperationException("保单配送信息删除失败");
			} 
		} catch (Exception e) {
			throw new DeliverInfoOperationException("Delete policy deliver Information error:" + e.getMessage());
		}
		return new DeliverInfoExecution(DeliverInfoOperationStateEnum.SUCCESS, deliverInfo);
	}

	@Override
	@Transactional
	public DeliverInfoExecution batchRemoveDeliverInfo(List<DeliverInfo> list) {
		// 空值判断
		if (list == null || list.size()<1) {
			return new DeliverInfoExecution(DeliverInfoOperationStateEnum.NULL);
		}
		List<Long> idList = new ArrayList<Long>();
		for (int i=0; i<list.size(); i++) {
			idList.add(list.get(i).getRecordID());			
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = deliverInfoDao.batchDeleteDeliverInfo(idList);
			if (effectedNum <= 0) {
				throw new DeliverInfoOperationException("保单配送信息批量删除失败");
			} 
		} catch (Exception e) {
			throw new DeliverInfoOperationException("Batch delete Policy deliver Information error:" + e.getMessage());
		}
		DeliverInfoExecution exe = new DeliverInfoExecution(DeliverInfoOperationStateEnum.SUCCESS);
		//将删除的记录总数加入到返回值
		exe.setCount(effectedNum);
		return exe;
	}

	@Override
	public DeliverInfo getTempDeliverInfo(Long recordID) {
		return deliverInfoImportDao.queryDeliverInfoByID(recordID);
	}

	@Override
	public DeliverInfoExecution getTempDeliverInfoList(DeliverInfo deliverInfoCondition, int pageIndex, int pageSize) {
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//依据查询条件，调用dao层返回相关的查询结果列表
		List<DeliverInfo> list = deliverInfoImportDao.queryDeliverInfoList(deliverInfoCondition,rowIndex,pageSize);
		//依据相同的查询条件，返回查询结果总数
		int count = deliverInfoImportDao.queryDeliverInfoCount(deliverInfoCondition);
		DeliverInfoExecution result = new DeliverInfoExecution();
		if (list != null) {
			result.setState(DeliverInfoOperationStateEnum.SUCCESS.getState());
			result.setStateInfo(DeliverInfoOperationStateEnum.SUCCESS.getStateInfo());
			result.setList(list);
			result.setCount(count);
		} else {
			result.setState(DeliverInfoOperationStateEnum.INNER_ERROR.getState());
		}
		return result;
	}

	@Override
	public DeliverInfoExecution addTempDeliverInfo(DeliverInfo deliverInfo) {
		// 空值判断
		if (deliverInfo == null) {
			return new DeliverInfoExecution(DeliverInfoOperationStateEnum.NULL);
		}
		int effectedNum;
		try {
			// 添加
			effectedNum = deliverInfoImportDao.insertDeliverInfo(deliverInfo);
			if (effectedNum <= 0) {
				throw new DeliverInfoOperationException("<ImportPool> 保单配送信息创建失败");
			} 
		} catch (Exception e) {
			throw new DeliverInfoOperationException("<ImportPool> Add deliverInfoImport Information error:" + e.getMessage());
		}
		return new DeliverInfoExecution(DeliverInfoOperationStateEnum.SUCCESS, deliverInfo);
	}

	@Override
	@Transactional
	public DeliverInfoExecution modifyTempDeliverInfo(DeliverInfo deliverInfo) {
		// 空值判断
		if (deliverInfo == null) {
			return new DeliverInfoExecution(DeliverInfoOperationStateEnum.NULL);
		}
		int effectedNum;
		// 即将更新信息
		try {
			// 更新操作
			effectedNum = deliverInfoImportDao.updateDeliverInfo(deliverInfo);
			if (effectedNum <= 0) {
				throw new DeliverInfoOperationException("<ImportPool> 保单配送信息更新失败");
			} 
		} catch (Exception e) {
			throw new DeliverInfoOperationException("<ImportPool> Modify deliver information import error:" + e.getMessage());
		}
		return new DeliverInfoExecution(DeliverInfoOperationStateEnum.SUCCESS, deliverInfo);
	}

	@Override
	@Transactional
	public DeliverInfoExecution removeTempDeliverInfo(DeliverInfo deliverInfo) {
		// 空值判断
		if (deliverInfo == null) {
			return new DeliverInfoExecution(DeliverInfoOperationStateEnum.NULL);
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = deliverInfoImportDao.deleteDeliverInfo(deliverInfo);
			if (effectedNum <= 0) {
				throw new DeliverInfoOperationException("<ImportPool> 保单临时信息删除失败");
			} 
		} catch (Exception e) {
			throw new DeliverInfoOperationException("<ImportPool> Delete deliver information import  error:" + e.getMessage());
		}
		return new DeliverInfoExecution(DeliverInfoOperationStateEnum.SUCCESS, deliverInfo);
	}

	@Override
	@Transactional
	public DeliverInfoExecution batchRemoveTempDeliverInfo(List<DeliverInfo> list) {
		// 空值判断
		if (list == null || list.size()<1) {
			return new DeliverInfoExecution(DeliverInfoOperationStateEnum.NULL);
		}
		List<Long> idList = new ArrayList<Long>();
		for (int i=0; i<list.size(); i++) {
			idList.add(list.get(i).getRecordID());			
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = deliverInfoImportDao.batchDeleteDeliverInfo(idList);
			if (effectedNum <= 0) {
				throw new DeliverInfoOperationException("<ImportPool> 保单临时配送信息批量删除失败");
			} 
		} catch (Exception e) {
			throw new DeliverInfoOperationException("<ImportPool> Batch delete deliver Information Import  error:" + e.getMessage());
		}
		DeliverInfoExecution exe = new DeliverInfoExecution(DeliverInfoOperationStateEnum.SUCCESS);
		//将删除的记录总数加入到返回值
		exe.setCount(effectedNum);
		return exe;
	}


}
